package com.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.exception.ModelNotFoundException;
import com.common.mapper.TagsMapper;
import com.common.model.Tags;
import com.common.service.TagsService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class TagsServiceImpl implements TagsService {

    private TagsMapper tagsMapper;

    public TagsServiceImpl(TagsMapper tagsMapper) {
        this.tagsMapper = tagsMapper;
    }

    @Override
    public void insert(Tags tags) {
        tagsMapper.insert(tags);
    }

    public Long selectTagsIdByName(String name) {

        QueryWrapper<Tags> qw = new QueryWrapper<>();
        qw.eq("name", name);

        Tags tags = tagsMapper.selectOne(qw);
        if (tags != null) {
            return tags.getTagsId();
        } else {
            Tags newTags = new Tags();
            newTags.setName(name);
            this.insert(newTags);
            return newTags.getTagsId();
        }
    }

    @Override
    public Map<Long, String> searchTags(String name) {

        QueryWrapper<Tags> qw = new QueryWrapper<>();
        if (name != null) {
            qw.like("name", name);
        }

        List<Tags> tags = tagsMapper.selectList(qw);
        Map<Long, String> result = new HashMap<>();
        tags.forEach(tag -> result.put(tag.getTagsId(), tag.getName()));

        return result;
    }

    @Override
    public String findAndInsertIdsWithNames(String tagNames) {

        if (tagNames != null) {

            String[] tags = tagNames.split(",");
            String[] tagIds = new String[tags.length];

            for (int i = 0; i < tags.length; i++) {
                String tag = tags[i];
                tagIds[i] = String.valueOf(this.selectTagsIdByName(tag));
            }

            return String.join(",", tagIds);
        } else {
            return "";
        }
    }

    @Override
    public String findTagsNamesOfIds(String tagIds) {

        if (tagIds != null) {
            QueryWrapper<Tags> qw = new QueryWrapper<>();
            qw.in("tags_id", tagIds.split(","));

            List<Tags> tags = tagsMapper.selectList(qw);
            LinkedList<String> tagNames = new LinkedList<>();

            tags.forEach(tag -> tagNames.add(tag.getName()));

            return String.join(",", tagNames);
        } else {
            return null;
        }
    }

    @Override
    public List<Tags> selectHostTags(Integer size) {
        QueryWrapper<Tags> qw = new QueryWrapper<>();
        qw.orderBy(true, false, "tags_id");

        IPage<Tags> tagsIPage = tagsMapper.selectPage(new Page<>(0, size), qw);

        return tagsIPage.getRecords();
    }

    @Override
    public List<Tags> selectAllTags() {

        QueryWrapper<Tags> qw = new QueryWrapper<>();
        qw.orderBy(true, false, "tags_id");

        return tagsMapper.selectList(qw);
    }

    @Override
    public Tags selectOneTagById(Long tagsId) {

        Tags tags = tagsMapper.selectById(tagsId);

        if (tags == null) {
            throw new ModelNotFoundException();
        }

        return tags;
    }
}
